<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
</body>
<script src="https://unpkg.com/vue@next"></script>
<script>
    const app = Vue.createApp({
        data() {
            return {
                name: 'andy',
                age: 18
            }
        },
        rules: {
            name: {
                validate: name => name.length > 6,
                message: 'name is too short'
            },
            age: {
                validate: age => age > 16, 
                message: 'to young, to simple'
            }
        },
        template: `
            <div>
               姓名：<input v-model="name" /><br>
               年龄：<input v-model="age" />
            </div>
        `
    })
    app.mixin({
        created(){
            console.log(this.$options.rules)
            for (const key in this.$options.rules){
                const item = this.$options.rules[key];
                console.log(key,item)
                //value 指的是用户输入的内容，也是我们需要校验的内容
                console.log(key,(value) => {
                    const result = item.validate(value);
                    if(!result){//如果不符合条件则显示提示信息
                        console.log(item.message);
                    }

                })
            }

        }
    })
    const vm = app.mount("#app");
</script>
</html>